package com.gwl.concurrent.interrupt;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static java.lang.System.out; 

public class CloseResource {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws IOException, InterruptedException {
		ExecutorService exec = Executors.newCachedThreadPool(); 
		@SuppressWarnings("unused")
		ServerSocket socket = new ServerSocket(8080); 
		InputStream socketIn = new Socket("localhost", 8080).getInputStream(); 
		exec.execute(new IOBlocked(System.in)); 
		exec.execute(new IOBlocked(socketIn)); 
		TimeUnit.MILLISECONDS.sleep(100); 
		out.println("Shutting down all thread"); 
		exec.shutdownNow(); 
		out.println("Closing " + socketIn.getClass().getSimpleName()); 
		socketIn.close(); 
		TimeUnit.SECONDS.sleep(1); 
		out.println("Closing " + System.in.getClass().getSimpleName()); 
		System.in.close(); 
	}

}
